﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using InstantNaukriDAL;

public partial class BranchMaster : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                clsBranchMasterDAL objclsBranchMasterDAL = new clsBranchMasterDAL();
                FillBranchInfo();
                //FillddlState();
            }
            catch (Exception)
            {
                throw;
            }
        }
    }

    protected void btnsaveEmployeinfo_Click(object sender, EventArgs e)
    {
        try
        {
            
            if (Session["BranchId"] == null)
            {
                IN_TB_USER objIN_TB_USER = new IN_TB_USER();
                clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();
                //Saving User

                objIN_TB_USER.EMAIL = txtemail.Text;

                if (rbtnLoginId.SelectedValue == "1")
                {
                    objIN_TB_USER.LOGIN_ID = txtemail.Text;
                    objIN_TB_USER.PASSWORD = txtPassword.Text;
                }
                if (rbtnLoginId.SelectedValue == "2")
                {
                    objIN_TB_USER.LOGIN_ID = txtmobile.Text;
                    objIN_TB_USER.PASSWORD = txtPassword.Text;
                }
                objIN_TB_USER.NAME = txtBranchName.Text;
                objIN_TB_USER.MOBILE = txtmobile.Text;
                objIN_TB_USER.EMAIL = txtemail.Text;

                objIN_TB_USER.STATUS = 1;
                objIN_TB_USER.ROLEID = 5;
                objIN_TB_USER.DOB = Convert.ToDateTime(txtDob.Text);
                objIN_TB_USER.CREATED_BY = 1;
                int UserId = objclsUserLoginDAL.SaveUser(objIN_TB_USER);


                clsBranchMasterDAL objclsBranchMasterDAL = new clsBranchMasterDAL();
                IN_TB_BRANCH_MST objIN_TB_BRANCH_MST = new IN_TB_BRANCH_MST();
                objIN_TB_BRANCH_MST.USER_ID = UserId;
                objIN_TB_BRANCH_MST.PHONE = txtPhone.Text;
                objIN_TB_BRANCH_MST.BRANCH_NAME = txtBranchName.Text;
                objIN_TB_BRANCH_MST.BRANCH_DESCRIPTION = txtBranchDescription.Text;
                objIN_TB_BRANCH_MST.LOGIN_TILL = Convert.ToDateTime(txtlogintill.Text.ToString());
                objIN_TB_BRANCH_MST.DOWNLOAD_TILL = Convert.ToDateTime(txtdownloadtill.Text);
                objIN_TB_BRANCH_MST.MOBILE = txtmobile.Text;
                objIN_TB_BRANCH_MST.EMAIL = txtemail.Text;
                objIN_TB_BRANCH_MST.STATUS = 1;
                int BranchId = objclsBranchMasterDAL.SaveBranchMst(objIN_TB_BRANCH_MST);
                //ClearAllControls();
                FillBranchInfo();
            }
            else
            {
                IN_TB_USER objIN_TB_USER = new IN_TB_USER();
                clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();
                objIN_TB_USER = objclsUserLoginDAL.GetUserByBranchID(Convert.ToInt32(Session["BranchId"].ToString()));
                if (objIN_TB_USER != null)
                {
                    if (txtBranchName.Text != "")
                        objIN_TB_USER.NAME = txtBranchName.Text;
                    if (txtemail.Text != "")
                        objIN_TB_USER.EMAIL = txtemail.Text;
                    if (txtmobile.Text != "")
                        objIN_TB_USER.MOBILE = txtmobile.Text;
                    if (txtDob.Text != "")
                        objIN_TB_USER.DOB = Convert.ToDateTime(txtDob.Text);
                    if (txtPassword.Text != "")
                        objIN_TB_USER.PASSWORD = txtPassword.Text;
                    if (rbtnLoginId.SelectedValue == "1" && txtemail.Text != "")
                        objIN_TB_USER.LOGIN_ID = txtemail.Text;
                    if (rbtnLoginId.SelectedValue == "2" && txtmobile.Text != "")
                        objIN_TB_USER.LOGIN_ID = txtmobile.Text;
                    objclsUserLoginDAL = new clsUserLoginDAL();
                    int UserId = objclsUserLoginDAL.SaveUser(objIN_TB_USER);

                    clsBranchMasterDAL objclsBranchMasterDAL = new clsBranchMasterDAL();
                    IN_TB_BRANCH_MST objIN_TB_BRANCH_MST = new IN_TB_BRANCH_MST();
                    objIN_TB_BRANCH_MST.ID = Convert.ToInt32(Session["BranchId"].ToString());
                    objIN_TB_BRANCH_MST.BRANCH_NAME = txtBranchName.Text;
                    objIN_TB_BRANCH_MST.BRANCH_DESCRIPTION = txtBranchDescription.Text;
                    objIN_TB_BRANCH_MST.PHONE = txtPhone.Text;
                    objIN_TB_BRANCH_MST.MOBILE = txtmobile.Text;
                    objIN_TB_BRANCH_MST.EMAIL = txtemail.Text;
                    objIN_TB_BRANCH_MST.USER_ID = UserId;
                  
                    objIN_TB_BRANCH_MST.DOWNLOAD_TILL = Convert.ToDateTime(txtdownloadtill.Text);
                    objIN_TB_BRANCH_MST.LOGIN_TILL = Convert.ToDateTime(txtlogintill.Text);
                    int EmployeId = objclsBranchMasterDAL.SaveBranchMst(objIN_TB_BRANCH_MST);
                }
                FillBranchInfo();
                //ClearAllControls();
                Session["BranchId"] = null;
            }
        }

        catch (Exception)
        {
            throw;
        }




    }
    public void FillBranchInfo()
    
    {
        try
        {

             List<clsGetBranchInfo> objclsGetBranchInfoList = new List <clsGetBranchInfo>();
          
            clsBranchMasterDAL objclsBranchMasterDAL = new clsBranchMasterDAL();
           
            objclsGetBranchInfoList = objclsBranchMasterDAL.GetAllBranchFullInfo();
            if (objclsGetBranchInfoList != null)
            {
                grdEmploye.DataSource = objclsGetBranchInfoList;
                grdEmploye.DataBind();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }
    protected void grdEmploye_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdEmploye.PageIndex = e.NewPageIndex;
        FillBranchInfo();
    }
    protected void grdEmploye_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Edt")
            {
                int BranchID = Convert.ToInt32(e.CommandArgument.ToString());
                clsBranchMasterDAL objclsBranchMasterDAL = new clsBranchMasterDAL();
                IN_TB_BRANCH_MST objIN_TB_BRANCH_MST = new IN_TB_BRANCH_MST();

                IN_TB_USER ObjIN_TB_USER = new IN_TB_USER();

                clsUserLoginDAL objclsUserLoginDAL = new clsUserLoginDAL();
                ObjIN_TB_USER = objclsUserLoginDAL.GetUserByBranchID(BranchID);
                if (ObjIN_TB_USER != null)
                {
                    txtBranchName.Text = ObjIN_TB_USER.NAME;
                    txtemail.Text = ObjIN_TB_USER.EMAIL;
                    txtmobile.Text = ObjIN_TB_USER.MOBILE;
                    txtDob.Text = Convert.ToString(ObjIN_TB_USER.DOB);
                }

                objIN_TB_BRANCH_MST = objclsBranchMasterDAL.GetBranchById(BranchID);



                if (objIN_TB_BRANCH_MST != null)
                {

                    txtBranchName.Text = objIN_TB_BRANCH_MST.BRANCH_NAME;
                    txtBranchDescription.Text = objIN_TB_BRANCH_MST.BRANCH_DESCRIPTION;
                    DateTime dtill = (DateTime)objIN_TB_BRANCH_MST.DOWNLOAD_TILL;
                    txtdownloadtill.Text = dtill.ToString("dd-MMM-yyyy");
                    DateTime ltill = (DateTime)objIN_TB_BRANCH_MST.LOGIN_TILL;
                    txtlogintill.Text = ltill.ToString("dd-MMM-yyyy");
                    txtemail.Text = objIN_TB_BRANCH_MST.EMAIL;
                    txtPhone.Text = objIN_TB_BRANCH_MST.PHONE;
                    txtmobile.Text = objIN_TB_BRANCH_MST.MOBILE;
                    txtPhone.Text = objIN_TB_BRANCH_MST.PHONE;
                    Session["BranchId"] = BranchID;


                }
            }

            if (e.CommandName == "Del")
            {
                int BranchId = Convert.ToInt32(e.CommandArgument.ToString());
                clsBranchMasterDAL objclsBranchMasterDAL = new clsBranchMasterDAL();
                IN_TB_BRANCH_MST objIN_TB_BRANCH_MST = new IN_TB_BRANCH_MST();
                objclsBranchMasterDAL.DeleteBranch(BranchId);
                FillBranchInfo();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }

}
